<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\controller\ding;

use app\store\controller\Controller;
use app\store\model\ding\Order as OrderModel;
use think\facade\Db;

/**
 * 商品分类
 * Class Category
 * @package app\store\controller\goods
 */
class Order extends Controller
{

    /**
     * 用户预订列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list($dataType)
    {
        $model = new OrderModel;
        $list  = $model->getListUser($dataType,$this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 清台
     * @param $OrderId
     * @return array
     * @throws \Exception
     */
    public function delete(int $orderId)
    {
        // 分类详情
        $model = OrderModel::detail($orderId);
        if (! $model->remove()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    //预订
    public function add()
    {
        // 新增记录
        $model = new OrderModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
    //预订
    public function edit(int $orderId)
    {
        $model = OrderModel::detail($orderId);
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('修改成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
    /**
     * 订单详情
     * @param int $orderId
     * @return array
     */
    public function detail(int $orderId)
    {
        // $foods = Db::name('ding_order_food')->select()->toArray();
        // foreach ($foods as $key => $value) {
        //     $food = Db::name('ding_food')->where('food_id', $value['food_id'])->find();
        //     if ($food) {
        //         Db::name('ding_order_food')->where('order_food_id', $value['order_food_id'])->update(['food_name' => $food['name']]);
        //     }
        // }

        // 订单详情
        $model = new OrderModel;
        if (! $detail = $model->getDetail($orderId)) {
            return $this->renderError('未找到该订单记录');
        }
        $goods                 = $detail['goods'];
        $detail['menu_price0'] = 0;
        $detail['menu_price1'] = 0;
        foreach ($goods as $key => $value) {
            if ($value['status'] == 1) {
                $detail['menu_price1'] += $value['total_pay_price'];
            } else {
                $detail['menu_price0'] += $value['total_pay_price'];
            }
        }
        return $this->renderSuccess(compact('detail'));
    }

}
